/**
 * 注册
 */
import { registerService } from '../../../model/service/account/register';
import { validatePhnoeNubmer, UUID36 } from '../../../util/index';

export default async function register(ctx: Context, next: Next): Promise<void> {
  const { body } = ctx.request;
  // 按理来说后台也应该对用户名和手机号再次进行检测，为了爽先不写了
  const { phoneNumber } = body;
  if (!validatePhnoeNubmer(phoneNumber)) {
    ctx.api.fail(-1, 'Invalid phone number');
  } else {
    body.uid = UUID36(10);
    const responseData = await registerService(body);
    ctx.api.success(responseData, 'sucess');
  }
  
  return next();
}